package com.hueydink.rhythmrouletteclient;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.ScrolledComposite;

public class AppWindow {

	public Display display;
	public StyledText styledText;
	public static String hostName;
	public static int portNumber;
	private static StyledText styledText_1;
	private static Text scoreTxt;
	private MessageProcessor messageProcessor = new MessageProcessor(this);

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("args[0] == " + args[0] + "\n" + "args[1] == "
				+ args[1]);
		hostName = args[0];
		portNumber = Integer.parseInt(args[1]);

		try {
			AppWindow window = new AppWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		Shell shlRhythmRouletteTest = new Shell();
		shlRhythmRouletteTest.setBackground(SWTResourceManager.getColor(0, 0,
				128));
		shlRhythmRouletteTest.setSize(769, 323);
		shlRhythmRouletteTest.setText("Rhythm Roulette Test Client");
		Button playRequestBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		playRequestBtn.addSelectionListener(new SelectionAdapter() {
			// playRequestBtn event handler
			public void widgetSelected(SelectionEvent e) {
				messageProcessor.sendPlayRequestMsg();
			}
		});
		playRequestBtn.setBounds(602, 10, 142, 30);
		playRequestBtn.setText("Start A New Game");

		Button resultBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		resultBtn.addSelectionListener(new SelectionAdapter() {
			// resutlBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String scoreString = scoreTxt.getText();
				scoreTxt.setText("");
				messageProcessor.sendResultMsg(scoreString);
			}
		});
		resultBtn.setBounds(602, 96, 142, 30);
		resultBtn.setText("Send Result");

		Button quitRequestBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		quitRequestBtn.addSelectionListener(new SelectionAdapter() {
			// quitRequestBtn event handler
			public void widgetSelected(SelectionEvent e) {
				messageProcessor.sendQuitRequestMsg();
			}
		});
		quitRequestBtn.setBounds(602, 236, 142, 30);
		quitRequestBtn.setText("Log Off");

		scoreTxt = new Text(shlRhythmRouletteTest, SWT.BORDER);
		scoreTxt.setBounds(602, 64, 142, 26);

		Button sendMsgBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		sendMsgBtn.addSelectionListener(new SelectionAdapter() {
			// sendMsgBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String phraseString = styledText_1.getText();
				styledText_1.setText("");
				messageProcessor.sendPhraseMsg(phraseString);
			}
		});

		sendMsgBtn.setBounds(602, 184, 142, 30);
		sendMsgBtn.setText("Send Phrase");

		ScrolledComposite scrolledComposite = new ScrolledComposite(
				shlRhythmRouletteTest, SWT.BORDER | SWT.V_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(true);
		scrolledComposite.setBounds(0, 0, 596, 278);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		styledText = new StyledText(scrolledComposite, SWT.BORDER
				| SWT.FULL_SELECTION | SWT.WRAP);
		styledText.setEditable(false);
		styledText.setForeground(SWTResourceManager.getColor(0, 102, 255));
		styledText.setFont(SWTResourceManager
				.getFont("Consolas", 9, SWT.NORMAL));
		styledText.setTopMargin(10);
		styledText.setRightMargin(10);
		styledText.setLeftMargin(10);
		styledText.setBottomMargin(10);
		styledText.setBackground(SWTResourceManager.getColor(0, 0, 0));
		scrolledComposite.setContent(styledText);
		scrolledComposite.setMinSize(styledText.computeSize(SWT.DEFAULT,
				SWT.DEFAULT));

		styledText_1 = new StyledText(shlRhythmRouletteTest, SWT.BORDER);
		styledText_1.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		styledText_1.setBottomMargin(3);
		styledText_1.setTopMargin(3);
		styledText_1.setRightMargin(3);
		styledText_1.setLeftMargin(3);
		styledText_1.setFont(SWTResourceManager.getFont("Fixedsys", 9,
				SWT.NORMAL));
		styledText_1.setBounds(602, 148, 142, 30);
		styledText_1.setWordWrap(true);
		shlRhythmRouletteTest.open();
		shlRhythmRouletteTest.layout();

		while (!shlRhythmRouletteTest.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

}
